﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Net.Mail;
using System.Text;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace NPSoft.Ferrero.Framework.Utility
{
    public class MailHelper
    {
        SmtpClient _client = new SmtpClient();

        /// <summary>
        /// 账号
        /// </summary>
        public string Account { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tos"></param>
        /// <param name="ccs"></param>
        /// <param name="mail"></param>
        /// <returns></returns>
        public NPMessage SendBySMTP(IList<MailAddress> tos, IList<MailAddress> ccs, MailMessage mail)
        {
            NPMessage miRet = new NPMessage();
            _client.Credentials = new System.Net.NetworkCredential(this.Account, this.Password);
            //上述写你的Mail邮箱和密码   
            _client.Port = 25;//mail使用的端口   
            _client.Host = "mail.hostuc.com";
            //_client.EnableSsl = true;//经过ssl加密

            MailMessage msg = mail;
            msg.From = new MailAddress(mail.From.Address, mail.From.DisplayName, System.Text.Encoding.UTF8);
            msg.Subject = mail.Subject;//邮件标题   
            msg.SubjectEncoding = Encoding.UTF8;//邮件标题编码   
            msg.Body = mail.Body;//邮件内容   
            msg.BodyEncoding = Encoding.UTF8;//邮件内容编码   
            msg.IsBodyHtml = true;//是否是HTML邮件   
            msg.Priority = MailPriority.High;//邮件优先级   

            // 发送
            foreach (MailAddress to in tos)
            {
                MailAddress item = new MailAddress(to.Address, to.DisplayName);
                msg.To.Add(item);
            }

            // 抄送
            foreach (MailAddress cc in ccs)
            {
                MailAddress item = new MailAddress(cc.Address, cc.DisplayName);
                msg.CC.Add(item);
            }

            miRet = Send(msg);

            return miRet;
        }

        /// <summary>
        //
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        private NPMessage Send(MailMessage msg)
        {
            NPMessage miRet = new NPMessage()
            {
                Success = true,
                Message = "发送成功！"
            };
            object userState = "NewsPage";
            try
            {
                //_client.SendAsync(msg, userState);// 异步发送
                _client.Send(msg);
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                miRet = new NPMessage();
                miRet.Success = false;
                miRet.Add(ex.Message + " : " + ex.InnerException.Message);
            }
            return miRet;
        }


        public byte[] GetAttachStream(DataTable dt)
        {
            try
            {
                XSSFWorkbook xssfworkbook = new XSSFWorkbook();
                ISheet sheet = xssfworkbook.CreateSheet("ErrorMsg");

                IRow row = sheet.CreateRow(0);
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    ICell cell = row.CreateCell(i);
                    cell.SetCellValue(dt.Columns[i].ColumnName);
                }

                //数据  
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    IRow row1 = sheet.CreateRow(i + 1);
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        ICell cell = row1.CreateCell(j);
                        cell.SetCellValue(dt.Rows[i][j].ToString());
                    }
                }

                //转为字节数组  
                MemoryStream stream = new MemoryStream();
                xssfworkbook.Write(stream);
                byte[] bys = stream.ToArray();
                stream.Close();
                stream.Dispose();

                return bys;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}

